/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import business.LocationsBusiness;
import business.PlacesBusiness;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Locations;
import model.Places;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class MngPlacesBean {
    private PlacesBusiness placesBusiness = new PlacesBusiness();
    private LocationsBusiness locationsBusiness = new LocationsBusiness();
    private List<Locations> listLocal;
    private List<Integer> listPage;

    public List<Integer> getListPage() {
        return listPage;
    }

    public void setListPage(List<Integer> listPage) {
        this.listPage = listPage;
    }
    private Places place;

    public Places getPlace() {
        return place;
    }

    public void setPlace(Places place) {
        this.place = place;
    }

    public List<Locations> getListLocal() {
        return listLocal;
    }

    public void setListLocal(List<Locations> listLocal) {
        this.listLocal = listLocal;
    }
    private List<Places> listPlace;

    public List<Places> getListPlace() {
        return listPlace;
    }

    public void setListPlace(List<Places> listPlace) {
        this.listPlace = listPlace;
    }

    public String updatePlacesAction(Places place){
        placesBusiness.updatePlaces(place);
        listPlace = placesBusiness.listAllPlaces();
        return "managementplaces.xhtml";
    }

    public String addNewPlaceAction(){
        placesBusiness.newPlaces(place);
        listPlace = placesBusiness.listAllPlaces();
        return "managementplaces.xhtml";
    }

    /** Creates a new instance of MngPlacesBean */
    public MngPlacesBean() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        int pageID;
        if(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID") != null){
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID").toString());
        }else{
            pageID = Integer.parseInt(session.getAttribute("pageIDPlaces").toString());
        }
        session.setAttribute("pageIDPlaces", pageID);

        int index = placesBusiness.getRowInListAllPlaces();
        listPage = new ArrayList<Integer>();
        if (index / 4 < 1) {
            listPage.add(1);
        } else {
            if (index % 4 == 0) {
                for (int i = 1; i <= index / 4; i++) {
                    listPage.add(i);
                }
            } else {
                for (int i = 1; i <= index / 4 + 1; i++) {
                    listPage.add(i);
                }
            }
        }
        place = new Places();
        listPlace = placesBusiness.listPlaceByPage(pageID);
//        listPlace = placesBusiness.listAllPlaces();
        listLocal = locationsBusiness.listAllLocation();
    }

}
